package org.brisling.service.common;

import java.util.List;

import org.brisling.domain.sysman.inf.HierachicalBeanDataModeler;
import org.brisling.repository.inf.HierachicalRepository;
import org.brisling.util.JsonUtil;
import org.brisling.util.LongUtil;
import org.springframework.data.domain.Sort;

/**
 * 
 * 树结构业务基础service，所有树结构业务对象必须继承该service类
 * @author : jackson wang
 * @Date   : 2015年6月8日 下午8:17:42
 * @version: 1.0
 */
@SuppressWarnings("rawtypes")
public class BaseTreeService {

	private String openState="closed";
	
	private String closeState="open";
	
	private Sort sort= new Sort(new String[] { "sortedid" });
	
	public BaseTreeService(){}
	
	@SuppressWarnings("unchecked")
	public List<? extends HierachicalBeanDataModeler> getRootNode(HierachicalRepository repo){
		System.out.println(" hello ");
		
		
		List<? extends HierachicalBeanDataModeler> dept = repo.findByParentidIsNull(sort);
		
		if(dept!=null&&dept.size()>0){
			
			for(int i=0;i<dept.size();i++){
				List<? extends HierachicalBeanDataModeler> childrens = repo.findByParentid(dept.get(i).getId(),sort);
				
				if(childrens!=null&&childrens.size()>0){
					//children is not null					
					//Set set = new HashSet(Arrays.asList(childrens));
					dept.get(i).setState(openState);
				}else{
					//leaf node,childrens is null
					dept.get(i).setState(closeState);
				}
			}
			
		}
		
		return dept;
	}
	
	@SuppressWarnings("unchecked")
	public List<? extends HierachicalBeanDataModeler> getNode(HierachicalRepository repo,Long id){
		
		
		List<? extends HierachicalBeanDataModeler> list = repo.findByParentid(id,sort);
		for(int i=0;i<list.size();i++){
			List<? extends HierachicalBeanDataModeler> childrens = repo.findByParentid(list.get(i).getId(),sort);
			
			if(childrens!=null&&childrens.size()>0){
				//children is not null					
				//Set set = new HashSet(Arrays.asList(childrens));
				list.get(i).setState(openState);
			}else{
				//leaf node,childrens is null
				list.get(i).setState(closeState);
			}
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public <T extends HierachicalBeanDataModeler>T getSingleNode(HierachicalRepository repo,Long id){
		
		return (T) repo.findOne(id);
	}
	
	@SuppressWarnings({ "unchecked"})
	public <T extends HierachicalBeanDataModeler>T getSingleNode(HierachicalRepository repo,String tid){
		Long pid = LongUtil.getLongValue(tid, null);
		
		if(tid!=null){			
			return (T) repo.findOne(pid);
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public <T extends HierachicalBeanDataModeler> T save(HierachicalRepository repo,T t){
		return (T) repo.save(t);
	}
	
	@SuppressWarnings("unchecked")
	public String del(HierachicalRepository repo,Long id){
		repo.delete(id);
		return "200";
	}
	
	@SuppressWarnings("unchecked")
	public String del(HierachicalRepository repo,String id){
		Long _id = LongUtil.getLongValue(id, null);
		if(_id!=null){
			repo.delete(id);
			return "200";
		}else{
			return "id must not be null";
		}
		
	}
	
	public <T extends HierachicalBeanDataModeler<HierachicalBeanDataModeler>>T  bodyToObj (String body,Class<T> clazz){
		
		
		T t = JsonUtil.bodyToObj(body, clazz);
		
		if(t.getParent()!=null&&t.getParent().getId()==null)
			t.setParent(null);
		return t;		
		
	}
	
}
